/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package woowcrawler;

import java.sql.*;

/**
 *
 * @author wisley
 */
public class DBMananger_wow {

    String driver = "com.mysql.jdbc.Driver";
    String stringConexao = "jdbc:mysql://localhost:3306/woowgroup";
    String usuario = "root";
    String senha = "123456";

    //artigo-
    public int getIdArtigoInserted()
    {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("SELECT last_insert_id() artigo");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
 public int getIdAutorInserted()
    {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("SELECT last_insert_id() autor");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
 public int getIdPalavraChaveInserted()
    {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("SELECT last_insert_id() palavrachave");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
    public int insertArtigo(String titulo, String url, String abstrac, String isbn, String quantpagina) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into artigo(titulo,url,abstract,isb,quantpagina)"
                    + " values('" + titulo + "," + url + "," + abstrac + "," + isbn + "," + quantpagina + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getArtigos() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from artigo;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getArtigosById(int id) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from artigo where artigo.id =" + id + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //-------
    //artigopalavrachave

    public int insertArtigopalavrachave(int artigoid, int palavrachave) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into artigopalavrachave(artigoid,palavrachave)"
                    + " values('" + artigoid + "," + palavrachave + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
    //------------------

    //autor
    public int insertAutor(String nome) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into autor(nome)"
                    + " values('" + nome + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getAutores() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from autor;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getAutorById(int id) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from autor where autor.id =" + id + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    
    }

    public ResultSet getAutorByName(String nome) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from autor where autor.nome =" + nome + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    
    }

    //------------------------

    //autorartigo
    public int insertAutorArtigo(int autorid, int artigoid) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into autorartigo(autorid,artigoid)"
                    + " values('" + autorid + "," + artigoid + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
    //------------------------

    //centroid
    public int insertCentroid(int artigoid, String radical, String palavra, int quant) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into centroid(artigoid,radical,palavra,quant)"
                    + " values('" + artigoid + "," + radical + "," + palavra + "," + quant + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getCentroidByArtigoId(int artigoid) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from centroid where centroid.artigoid =" + artigoid + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //-------------------
    //consulta

    public int insertConsulta( int usuarioid, String conteudo) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into consulta(id,usuarioid,conteudo)"
                    + " values('" + usuarioid + "," + conteudo + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }
    public ResultSet getConsultaByUsuarioId(int usuarioid) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from centroid where consulta.usuarioid=" + usuarioid + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //---------------------

    //consultaPalavra
    public int insertConsultaPalavra(int consultaId, int palavraId) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into consultapalavra(consultaid, palavraid)"
                    + " values('" + consultaId + "," + palavraId + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getConsultaPalavra() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from consultapalavra;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getConsultaPalavraByIds(int consultaId, int palavraId) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from consultapalavra where consultapalavra.consultaid =" + consultaId + "and" + "consultapalavra.palavraid =" + palavraId + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //---------------------

    //palavra
    public int insertPalavra(String conteudo) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into palavra(conteudo)"
                    + " values('" + conteudo + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getPalavras() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from palavra;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getPalavraById(int id) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from palavra where palavra.id =" + id + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //------------

    //palavrachave
    public int insertPalavraChave(String conteudo) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into palavrachave(conteudo)"
                    + " values('" + conteudo + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getPalavraChave() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from palavrachave;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getPalavraChaveById(int id) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from palavrachave where palavra.id =" + id + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    public ResultSet getPalavraChaveByName(String tag) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from palavrachave where palavrachave.nome =" + tag + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;

    }
    //-------------------

    //usuario
    public int insertUsuario(String nome) {
        int r = 0;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            r = comando.executeUpdate("insert into usuario(nome)"
                    + " values('" + nome + "');");

        } catch (Exception x) {
            System.out.println(x.getMessage());
        }
        return r;
    }

    public ResultSet getUsuarios() {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from usuario;");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }

    public ResultSet getUsuarioById(int id) {
        ResultSet rs = null;

        Connection conexao;

        try {
            Class.forName(driver);

            conexao = DriverManager.getConnection(stringConexao, usuario, senha);

            Statement comando = conexao.createStatement();

            rs = comando.executeQuery("select * from usuario where usuario.id =" + id + ";");
        } catch (Exception x) {
            System.out.println(x.getMessage());
        }

        return rs;
    }
    //---------------------------
}
